/**
 * <p>文件名: SysOutAuthAction.java</p>
 * <p>版权声明: Copyright &copy; 2012-2013 天健通泰科技</p>
 * <p>创建者: 李数</p>
 * <p>创建时间: 2012-10-22  下午04:22:26</p>
 */
package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.system.bean.TdmOutauth;
import com.tjtt.tdm.system.service.SysOutAuthService;

/**
 * @author Administrator
 *
 */
public class SysOutAuthAction extends BaseAction{
	private SysOutAuthService sysOutAuthService;

	private String toUserid;
	private String startDate;
	private String endDate;
	private String operateModuleId;
	private String operateModuleName;
	private String flowId;
	/**
	 * 查询增量表数据
	 *  void
	 * @author 李数
	 * @date 2012-9-24 上午09:49:14
	 */
	public void queryOutAuth(){
		try {
			Map<String, String> filterMap = getPagerMap();
			String start_date_from = getRequest().getParameter("start_date_from");
			String start_date_to = getRequest().getParameter("start_date_to");
			String end_date_from = getRequest().getParameter("end_date_from");
			String end_date_to = getRequest().getParameter("end_date_to");
		    //取出页面中的查询条件
			filterMap.put("userId", getUserId());
			filterMap.put("start_date_from", start_date_from);
			filterMap.put("start_date_to", start_date_to);
			filterMap.put("end_date_from", end_date_from);
			filterMap.put("end_date_to", end_date_to);
			String[] viewArray = { "id","from_user","module_name", "to_user",
					"start_date","end_date"};
			// 查询所有的数据
			PageResult result = sysOutAuthService.queryOutauth(filterMap);
			String returnStr = getColumnJson(result, viewArray);

			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 跳转添加授权页面
	 * @return String
	 * @author 李数
	 * @date 2012-10-22 下午05:21:31
	 */
	public String gotoAddAuth() {
		try {
			String haveAuth = sysOutAuthService.haveOutauth(getUserId());
			getRequest().setAttribute("haveAuth", haveAuth);
			return "add";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	/**
	 * 保存外出授权
	 *  void
	 * @author 李数
	 * @date 2012-10-22 下午05:54:25
	 */
	public String saveOutauth() throws Exception{
		try {
			TdmOutauth auth = new TdmOutauth();
//			String []operateModuleIds = operateModuleId.split(",");
//			String []operateModuleNames = operateModuleName.split(",");
//			for(int i = 0;i <operateModuleIds.length;i++){
			auth.setCreateDate(new Timestamp(System.currentTimeMillis()));
			auth.setFlowId(Long.parseLong(flowId));
			auth.setAuthState(new BigDecimal("0"));
			auth.setFromUserid(new BigDecimal(getUserId()));
			auth.setToUserid(new BigDecimal(toUserid));
			auth.setOperateModuleId(operateModuleId);
			auth.setOperateModulename(operateModuleName);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = df.parse(startDate + " 00:00:00");
			Timestamp ts = new Timestamp(date.getTime());
			auth.setStartDate(ts);
			Timestamp tss = new Timestamp(df.parse(endDate + " 23:59:59").getTime());
			auth.setEndDate(tss);
			sysOutAuthService.saveOutauth(auth);
//			}
			getRequest().setAttribute("message", "添加成功");
			return "gotosave";
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	/**
	 * 查询所有的模块
	 * @throws Exception void
	 * @author 李数
	 * @date 2012-10-22 下午05:56:28
	 */
	public void getAllModule() throws Exception{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		try {
			String haveAuth = sysOutAuthService.haveOutauth(getUserId());
			String allModule = sysOutAuthService.getModule(getRole(), haveAuth);
			out.print(allModule);
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
	}
	
	public void deleteOutAuth() throws Exception {
		setContentType();
		PrintWriter out = getResponse().getWriter();
		try {
			String ids = getRequest().getParameter("ids");
			sysOutAuthService.deleteOutauth(ids);
			out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
	}
	
	
	/**
	 * 查询待办授权
	 *  void
	 * @author 李数
	 * @date 2012-9-24 上午09:49:14
	 */
	public void queryOutauthList(){
		try {
			String flowtypes = getRequest().getParameter("FLOWNAME");
			String flownames = getRequest().getParameter("FLOW_NAME");
			String flowtype="";
			String flowname="";
			String WEITUODANWEI = StringUtil.encodingUrl(getRequest().getParameter("WEITUODANWEI"));
			String TEST_NO = StringUtil.encodingUrl(getRequest().getParameter("TEST_NO"));
			String WEITUORIQ = StringUtil.encodingUrl(getRequest().getParameter("WEITUORIQ"));
			String WEITUORIQTO = StringUtil.encodingUrl(getRequest().getParameter("WEITUORIQTO"));
			Map<String, String> filterMap = getPagerMap();
			if(!"".equals(flowtypes)&&flowtypes!=null){
				flowtype = java.net.URLDecoder.decode(flowtypes,"UTF-8");
			}
			if(!"".equals(flownames)&&flownames!=null){
							flowname = java.net.URLDecoder.decode(flownames,"UTF-8");
						}	
			filterMap.put("flowtype", flowtype);					
			filterMap.put("flowname", flowname);
			filterMap.put("WEITUODANWEI", WEITUODANWEI);
			filterMap.put("TEST_NO", TEST_NO);
			filterMap.put("WEITUORIQ", WEITUORIQ);
			filterMap.put("WEITUORIQTO", WEITUORIQTO);
			String[] viewArray = { "ID","FLOWNAME","FLOW_NAME", "FLOW_TYPE_ID", "FLOW_STATE_ID","FLOW_STATE_NAME","FLOW_ACTION_TYPE","INSTANCE_ID","NEXT_ROLES_ID",
					"NEXT_ROLES_NAME","NEXT_USERS_ID","NEXT_USERS_NAME","ADD_TIME","ADD_USER_ID","ADD_USER_NAME","TEST_NO"};
			// 查询所有的数据
			PageResult result = sysOutAuthService.queryOutauthList(filterMap);
			String returnStr = getColumnJson(result, viewArray);

			setContentType();
			PrintWriter out = getResponse().getWriter();
			System.out.println(returnStr);
			out.print(returnStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public SysOutAuthService getSysOutAuthService() {
		return sysOutAuthService;
	}

	public void setSysOutAuthService(SysOutAuthService sysOutAuthService) {
		this.sysOutAuthService = sysOutAuthService;
	}
	public String getToUserid() {
		return toUserid;
	}
	public void setToUserid(String toUserid) {
		this.toUserid = toUserid;
	}
	public String getStartDate() {
		return startDate;
	}
	public void setStartDate(String startDate) {
		this.startDate = startDate;
	}
	public String getEndDate() {
		return endDate;
	}
	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}
	public String getOperateModuleId() {
		return operateModuleId;
	}
	public void setOperateModuleId(String operateModuleId) {
		this.operateModuleId = operateModuleId;
	}
	public String getFlowId() {
		return flowId;
	}
	public void setFlowId(String flowId) {
		this.flowId = flowId;
	}
	public String getOperateModuleName() {
		return operateModuleName;
	}
	public void setOperateModuleName(String operateModuleName) {
		this.operateModuleName = operateModuleName;
	}
	
}
